package com.hsy.linemoney.modules.tender.activity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.deparse.dpframe.tools.utils.DPGsonUtil;
import com.deparse.dpframe.ui.common.DPBusEvent;
import com.hsy.linemoney.R;
import com.hsy.linemoney.apiservice.BidService;
import com.hsy.linemoney.apiservice.CalculateService;
import com.hsy.linemoney.apiservice.Callback;
import com.hsy.linemoney.apiservice.CurrencyService;
import com.hsy.linemoney.apiservice.LineMoneyContext;
import com.hsy.linemoney.common.Consts;
import com.hsy.linemoney.modules.common.CommonBaseActivityWithSimpleActionBar;
import com.hsy.linemoney.modules.common.EditTextExt;
import com.hsy.linemoney.modules.common.StepView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/7/1.
 */
public class TenderStep1Activity extends CommonBaseActivityWithSimpleActionBar implements View.OnClickListener {

    protected StepView stepView;
    protected TextView orderSn;
    protected TextView orderTime;
    protected TextView orderMoney;
    protected TextView orderDuration;
    protected TextView orderRate;
    protected TextView moneyGetLabel;
    protected TextView moneyGetUnit;
    protected TextView moneyGetPrice;
    protected TextView txtBidCostUnit;
    protected TextView txtBidCostPrice;
    protected TextView txtDealMoneyUnit;
    protected TextView txtDealMoneyPrice;
    protected LinearLayout resultPanel;
    protected TextView nextText;
    protected EditTextExt bidAmount;
    private CurrencyService.CurrencyListResult mCurrencyListResult;
    private CalculateService.CalculateResult mCalculateResult;
    private BidService.FromCurrencyToCurrencyOrderResult.Value mFromCurrencyToCurrencyOrderResultValue;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.detail_tender);
        setTitle(R.string.title_detail_tender);
        showProgressDialog(this);
        mFromCurrencyToCurrencyOrderResultValue = DPGsonUtil.fromJson(getActivityStartExtra()[0], BidService.FromCurrencyToCurrencyOrderResult.Value.class);

        new CurrencyService().getCurrency(new Callback<CurrencyService.CurrencyListResult>() {
            @Override
            public void onSuccess(int what, CurrencyService.CurrencyListResult result) {
                TenderStep1Activity.this.mCurrencyListResult = result;
            }

            @Override
            public void onError(int what, int type, String msg, Exception e) {

            }
        });
        initView();
        initData();

        BidService.FromCurrencyToCurrencyOrderResult.Value value = DPGsonUtil.fromJson(getActivityStartExtra()[0], BidService.FromCurrencyToCurrencyOrderResult.Value.class);
        CalculateService calculateService = new CalculateService();
        calculateService.calculate(value.getToCurrencyCd(), value.getFromCurrencyCd(), orderMoney.getText().toString(), "0", value.getServiceTypeCd(), orderRate.getText().toString(), new Callback<CalculateService.CalculateResult>() {
            @Override
            public void onSuccess(int what, CalculateService.CalculateResult result) {
                TenderStep1Activity.this.mCalculateResult = result;
                String des = "";
                String lang = LineMoneyContext.getLocalLanguagePair().locale.getLanguage();
                String currencyCd = mFromCurrencyToCurrencyOrderResultValue.getToCurrencyCd();
                if (lang.equalsIgnoreCase("en") || lang.equalsIgnoreCase("english")) {
                    if (currencyCd.equalsIgnoreCase("CNY")) {
                        des = "RMB";
                    } else {
                        des = "USD";
                    }
                } else {
                    if (currencyCd.equalsIgnoreCase("CNY")) {
                        des = "人民币";
                    } else {
                        des = "美元";
                    }
                }
                String unit = currencyCd.equalsIgnoreCase("CNY") ? "￥" : "$";

//                            moneyGetLabel.setText(String.format(LineMoneyContext.getLocalLanguagePair().locale, getString(R.string.txt_zheherenminbi), des));
//                            moneyGetUnit.setText(unit);
//                            moneyGetPrice.setText(result.getData().getLmData().getLinemoneyPayOut());
//                            txtBidCostUnit.setText(unit);
//                            txtBidCostPrice.setText(result.getData().getLmData().getLinemoneyTransferFee());
//                            txtDealMoneyUnit.setText(unit);
//                            txtDealMoneyPrice.setText(result.getData().getLmData().getLinemoneyPayOut());
//                            resultPanel.setVisibility(View.VISIBLE);
                hideProgressDialog();
            }

            @Override
            public void onError(int what, int type, String msg, Exception e) {
                showToast(R.string.txt_network_failure);
                hideProgressDialog();
            }
        });
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.commit && checkThrought()) {
            BidService.FromCurrencyToCurrencyOrderResult.Value value = DPGsonUtil.fromJson(getActivityStartExtra()[0], BidService.FromCurrencyToCurrencyOrderResult.Value.class);

            BidOrderParams.BidOrders orders = new BidOrderParams.BidOrders();
            orders.setBidAmount(orderMoney.getText().toString());
            orders.setBidRate(value.getFxRate());
            orders.setHostOrderId(value.getOrderId());

            BidOrderParams.BidRequestJson json = new BidOrderParams.BidRequestJson();
            json.setLanguageCd(LineMoneyContext.getLocalLanguagePair().locale.getLanguage());
            json.setUserId(LineMoneyContext.getLoggedUser().getUserId());
            json.setFromCurrencyCd(value.getFromCurrencyCd());
            json.setFromCurrencyCdUnit(mCurrencyListResult.getCurrencyDescription(value.getFromCurrencyCd()));
            json.setToCurrencyCd(value.getToCurrencyCd());
            json.setToCurrencyCdUnit(mCurrencyListResult.getCurrencyDescription(value.getToCurrencyCd()));
            json.setServiceTypeCd(value.getServiceTypeCd());
            json.setFxRate(value.getFxRate());

            List<BidOrderParams.BidOrders> orderses = new ArrayList<>();
            orderses.add(orders);
            json.setBidOrders(orderses);
            startActivity(TenderStep2Activity.class, DPGsonUtil.toJson(json), DPGsonUtil.toJson(mCalculateResult));
        }
    }

    private boolean checkThrought() {
        if (null == mCurrencyListResult || null == mCalculateResult) {
            return false;
        }
//        else if (TextUtils.isEmpty(bidAmount.getText())) {
//            showToast(R.string.txt_bid_amount_cannot_be_empty);
//            return false;
//        }
        return true;
    }

    private void initView() {
        stepView = (StepView) findViewById(R.id.step_view);
        orderSn = (TextView) findViewById(R.id.order_sn);
        orderTime = (TextView) findViewById(R.id.order_time);
        orderMoney = (TextView) findViewById(R.id.order_money);
        orderDuration = (TextView) findViewById(R.id.order_duration);
        orderRate = (TextView) findViewById(R.id.order_rate);
        moneyGetLabel = (TextView) findViewById(R.id.money_get_label);
        moneyGetUnit = (TextView) findViewById(R.id.money_get_unit);
        moneyGetPrice = (TextView) findViewById(R.id.money_get_price);
        txtBidCostUnit = (TextView) findViewById(R.id.txt_bid_cost_unit);
        txtBidCostPrice = (TextView) findViewById(R.id.txt_bid_cost_price);
        txtDealMoneyUnit = (TextView) findViewById(R.id.txt_deal_money_unit);
        txtDealMoneyPrice = (TextView) findViewById(R.id.txt_deal_money_price);
        resultPanel = (LinearLayout) findViewById(R.id.result_panel);
        nextText = (TextView) findViewById(R.id.commit);
        nextText.setOnClickListener(TenderStep1Activity.this);
        bidAmount = (EditTextExt) findViewById(R.id.bid_amount);
//        bidAmount.addTextChangedListener(new TextWatcher() {
//            @Override
//            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//
//            }
//
//            @Override
//            public void onTextChanged(CharSequence s, int start, int before, int count) {
//
//            }
//
//            @SuppressLint("StringFormatInvalid")
//            @Override
//            public void afterTextChanged(Editable s) {
//                if (0 == s.length()) {
        resultPanel.setVisibility(View.GONE);
//                } else {
//                    BidService.FromCurrencyToCurrencyOrderResult.Value value = DPGsonUtil.fromJson(getActivityStartExtra()[0], BidService.FromCurrencyToCurrencyOrderResult.Value.class);
//                    CalculateService calculateService = new CalculateService();
//                    calculateService.calculate(value.getToCurrencyCd(), value.getFromCurrencyCd(), bidAmount.getText().toString(), "0", value.getServiceTypeCd(), "0", new Callback<CalculateService.CalculateResult>() {
//                        @Override
//                        public void onSuccess(int what, CalculateService.CalculateResult result) {
//                            TenderStep1Activity.this.mCalculateResult = result;
//                            String des = "";
//                            String lang = LineMoneyContext.getLocalLanguagePair().locale.getLanguage();
//                            String currencyCd = mFromCurrencyToCurrencyOrderResultValue.getToCurrencyCd();
//                            if (lang.equalsIgnoreCase("en") || lang.equalsIgnoreCase("english")) {
//                                if (currencyCd.equalsIgnoreCase("CNY")) {
//                                    des = "RMB";
//                                } else {
//                                    des = "USD";
//                                }
//                            } else {
//                                if (currencyCd.equalsIgnoreCase("CNY")) {
//                                    des = "人民币";
//                                } else {
//                                    des = "美元";
//                                }
//                            }
//                            String unit = currencyCd.equalsIgnoreCase("CNY") ? "￥" : "$";
//
////                            moneyGetLabel.setText(String.format(LineMoneyContext.getLocalLanguagePair().locale, getString(R.string.txt_zheherenminbi), des));
////                            moneyGetUnit.setText(unit);
////                            moneyGetPrice.setText(result.getData().getLmData().getLinemoneyPayOut());
////                            txtBidCostUnit.setText(unit);
////                            txtBidCostPrice.setText(result.getData().getLmData().getLinemoneyTransferFee());
////                            txtDealMoneyUnit.setText(unit);
////                            txtDealMoneyPrice.setText(result.getData().getLmData().getLinemoneyPayOut());
////                            resultPanel.setVisibility(View.VISIBLE);
//                        }
//
//                        @Override
//                        public void onError(int what, int type, String msg, Exception e) {
//                            showToast(R.string.txt_network_failure);
//                        }
//                    });
//                }
//            }
//        });
    }

    private void initData() {
        BidService.FromCurrencyToCurrencyOrderResult.Value value = DPGsonUtil.fromJson(getActivityStartExtra()[0], BidService.FromCurrencyToCurrencyOrderResult.Value.class);
        orderSn.setText(getString(R.string.txt_order_sn, value.getOrderId()));
        orderTime.setText(value.getOrderTime());
        orderMoney.setText(value.getFromAmount());
        orderDuration.setText(value.getServiceTypeDesc());
        orderRate.setText(value.getFxRate());
    }

    @Override
    public void onEventMainThread(DPBusEvent event) {
        super.onEventMainThread(event);
        if (event.what == Consts.BusEventCommands.FINISH_EXCHANGE_STEP_2_AND_3) {
            finish();
        }
    }
}
